package com.cy.shiro.controller;

import com.cy.shiro.pojo.SysUser;
import com.cy.shiro.service.SysResourcesService;
import com.cy.shiro.service.SysUserService;
import com.cy.shiro.util.ResultUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员
 */
@Controller
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysResourcesService sysResourcesService;
    @Autowired
    private BaseController baseController;

    //管理员列表
    @RequestMapping("/users")
    public String users(Model model){

        //资源权限
        baseController.sysResources("用户列表","/users", model);

//        List<SysUser> sysUsers = sysUserService.selectAll();
//        System.out.println(sysUsers);
//        model.addAttribute("sysUsers", sysUsers);
        return "user/users";
    }


    /**
     * post请求获取用户列表
     * @return
     */
    @GetMapping("/users/list")
    @ResponseBody
    @CrossOrigin
    public Object userList(HttpServletRequest request){
        String limit = request.getParameter("limit") == null ? "10" : request.getParameter("limit");
        String offset = request.getParameter("offset");
        String page = request.getParameter("page");
        String sort = "desc";

        if(limit == null){
            limit = "10";
        }

//        System.out.println("limit:"+limit+",offset:"+offset+",page:"+page);

        Map<String, String> map = new HashMap<String, String>();
        map.put("limit", limit);
        map.put("offset", offset);
        map.put("page", page);
        map.put("sort", sort);
        map.put("username", request.getParameter("username"));
        map.put("nickname", request.getParameter("nickname"));
        map.put("mobile", request.getParameter("mobile"));
        map.put("email", request.getParameter("email"));
        map.put("qq", request.getParameter("qq"));

        PageHelper.startPage(Integer.valueOf(page),Integer.valueOf(limit));
        List<SysUser> rows = sysUserService.selectAll(map);

        PageInfo<SysUser> pageInfo = new PageInfo<>(rows);
        System.out.println("pageInfo:"+pageInfo.getTotal());

        int total = Integer.valueOf((int) pageInfo.getTotal());

        return  ResultUtil.bootstrapTableJson(pageInfo.getList(), total, map);
    }

    /**
     * 添加系统用户
     * @param request
     * @return
     */
    @RequiresPermissions("user:add")
    @PostMapping("/users/addPost")
    @ResponseBody
    @CrossOrigin
    public Object addPost(HttpServletRequest request){
        //接收参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String mobile   = request.getParameter("mobile");
        String email    = request.getParameter("email");
        String qq       = request.getParameter("qq");
        String gender   = request.getParameter("gender");
        String avatar   = request.getParameter("avatar");
        String remark   = request.getParameter("remark");

        //验证用户是否存在
        SysUser sysUser = sysUserService.findByUsername(username.trim());
        if(sysUser != null){
            return ResultUtil.error("此用户已存在");
        }

        //添加数据
        Map<String, String> map = new HashMap<>();
        map.put("username", username);
        map.put("password", password);
        map.put("nickname", nickname);
        map.put("mobile", mobile);
        map.put("email", email);
        map.put("qq", qq);
        map.put("gender", gender);
        map.put("avatar", avatar);
        map.put("remark", remark);

        //新增数据
        Integer id = sysUserService.addUser(map);
        if("0".equals(String.valueOf(id)) || "null".equals(String.valueOf(id))){
            return ResultUtil.error("添加失败");
        }

        return ResultUtil.success("success");
    }


    //修改
    @PostMapping("/users/editPost")
    @ResponseBody
    @CrossOrigin
    public Object editPost(SysUser sysUser){

        System.out.println("编辑用户信息："+ sysUser);
        //判断 todo
        //保存
        Integer id = sysUserService.editUser(sysUser);
        if("0".equals(String.valueOf(id)) || "null".equals(String.valueOf(id))){
            return ResultUtil.error("修改失败");
        }
        return ResultUtil.success("修改成功");
    }

    //删除用户
    @PostMapping("/users/delUser")
    @ResponseBody
    @CrossOrigin
    public Object delUser(HttpServletRequest request){
        String id = request.getParameter("id");
        if("0".equals(String.valueOf(id)) || "null".equals(String.valueOf(id))){
            return ResultUtil.error("修改失败");
        }
        //转为整数
        Integer userId = Integer.valueOf(id);

        //删除
        Integer integer = sysUserService.delUser(userId);
        if("0".equals(String.valueOf(integer)) || "null".equals(String.valueOf(integer))){
            return ResultUtil.error("删除失败");
        }

        return ResultUtil.success("删除成功");
    }
}
